package com.pachong.android.frameworkbase.customviews;


import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.pachong.android.frameworkbase.utils.DateTimeUtil;

import java.util.Calendar;
import java.util.Date;

/**
 * 一个可以显示和设置日期时间的bar
 * 
 * @author Veivei Peng
 * 
 */
public class DateTimeBar extends LinearLayout {
	private static final String TAG = DateTimeBar.class.getSimpleName();
	private TextView mBtnDate = null;
	private TextView mBtnTime = null;
	private Date mMyDate = Calendar.getInstance().getTime();
	private Date mMyTime = Calendar.getInstance().getTime();

	public DateTimeBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOrientation(HORIZONTAL);
		float textSize = 16;
		// Date button
		int dateBtnTextColor = Color.parseColor("#808080");
		mBtnDate = new TextView(context);
		mBtnDate.setSingleLine();
		mBtnDate.setText(DateTimeUtil.getCurrDateStr());
		mBtnDate.setTextColor(dateBtnTextColor);
		mBtnDate.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
		mBtnDate.setBackgroundColor(getResources().getColor(android.R.color.transparent));
		mBtnDate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DateTimeUtil.showDateSettingDialog(mBtnDate.getContext(), mMyDate, new DatePickerDialog.OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker view, int year, int monthOfYear,
							int dayOfMonth) {
						Calendar cal = Calendar.getInstance();
						cal.set(year, monthOfYear, dayOfMonth);
						String dateStr = DateTimeUtil.getDateStr(cal.getTime());
						mBtnDate.setText(dateStr);
						mMyDate = cal.getTime();
					}
				});

			}
		});
		LayoutParams params = new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		this.addView(mBtnDate, params);

		// Time button
		int timeBtnTextColor = Color.parseColor("#808080");
		mBtnTime = new TextView(context);
		mBtnTime.setSingleLine();
		mBtnTime.setBackgroundColor(getResources().getColor(android.R.color.transparent));
		mBtnTime.setTextColor(timeBtnTextColor);
		mBtnTime.setTextSize(textSize);
		mBtnTime.setText(DateTimeUtil.getCurrTimeStr());
		mBtnTime.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DateTimeUtil.showTimeSettingDialog(mBtnTime.getContext(), mMyTime, new TimePickerDialog.OnTimeSetListener() {

					@Override
					public void onTimeSet(TimePicker view, int hourOfDay,
							int minute) {
						Calendar cal = Calendar.getInstance();
						cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
						cal.set(Calendar.MINUTE, minute);

						mBtnTime.setText(DateTimeUtil.getTimeStr(cal.getTime()));
						mMyTime = cal.getTime();

					}
				});

			}
		});


		params = new LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		params.setMargins(10, 0, 0, 0);
		this.addView(mBtnTime, params);

	}

	public void setDate(Date date) {
		if (date != null) {
			String dateStr = DateTimeUtil.getDateStr(date);
			mBtnDate.setText(dateStr);
			mMyDate = date;
		}
	}
	
	public void setTime(Date date) {
		if (date != null) {
			String timeStr = DateTimeUtil.getTimeStr(date);
			mBtnTime.setText(timeStr);
			mMyTime = date;
		}
	}
	
	public Date getDate() {
		return mMyDate;
	}
	
	public Date getTime() {
		return mMyTime;
	}
	
	public Date getDateAndTime() {
		Date date = (Date)mMyDate.clone();
		date.setHours(mMyTime.getHours());
		date.setMinutes(mMyTime.getMinutes());
		return date;
	}
	
	public void setDateAndTime(Date date) {
		setDate(date);
		setTime(date);
	}

}
